package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.ClientExistException;

import java.io.Serializable;

public class Bank{
    private int numOfClients = 0;
    public Client[] clients;
    private String filePath;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    private ClientRegistrationListener[] listeners;

    public Bank(int numOfClients, ClientRegistrationListener[] listeners) {
        clients = new Client[numOfClients];
        this.listeners = listeners;
        listeners[0] = new EmailNotificationListener();
        listeners[1] = new PrintClientListener();
    }

    public void addClient(Client client) throws ClientExistException {
        for (Client c : clients) {
            if ( c != null ) {
                if ( c.getName().equals(client.getName())) {
                    throw new ClientExistException();
                }
            }
        }
        clients[numOfClients] = client;
        numOfClients++;

        for (ClientRegistrationListener l : listeners) {
            if ( l != null ) {
                l.onClientAdded(client);
            }
        }
    }

    public static class EmailNotificationListener implements ClientRegistrationListener{

        @Override
        public void onClientAdded(Client c) {
            System.out.println("Notification Email for client " + c.getName() + " to be sent");
        }
    }

    public static class PrintClientListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client c) {
            System.out.println("Client name is " + c.getName());
        }
    }
}
